/*
 * 2005-11-19 lxm 创建。 
 */
package com.hd123.rumba.organization;

import java.text.ParseException;
import java.util.List;

import net.sf.hibernate.HibernateException;

import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.runtime.DBaseEntity;
import com.hd123.rumba.runtime.IPersistent;
import com.hd123.rumba.runtime.NamingMgr;

/**
 * @author lxm
 * 
 * 组织，数据交换对象
 */
public class DOrganization extends DBaseEntity {

  public static String CAPTION_CLASS = "组织数据交换";
  
  private String code;
  private String name;
  private String organizationDomainUuid;
  private String upperOrganizationUuid;
  private String remark;
  private String levelId;
  private DOrgRole[] orgRoles;

  // /////////////////////////
  // 用于 Web Service 数据交换

  public void assignFrom(IPersistent source) {
    super.assignFrom(source);
    if (source instanceof IOrganization) {
      this.code = exportString(source, "getCode");
      this.name = exportString(source, "getName");
      this.organizationDomainUuid = exportString(source, new String[] {
          "getOrganizationDomain", "getUuid" });
      this.upperOrganizationUuid = exportString(source, new String[] {
          "getUpperOrganization", "getUuid" });
      this.remark = exportString(source, "getRemark");
      this.levelId = exportString(source, "getLevelId");

      // orgRoles
      try {
        List ors = ((IOrganization) source).getOrgRoles();
        this.orgRoles = new DOrgRole[ors.size()];
        for (int i = 0; i < ors.size(); i++) {
          IOrgRole role = (IOrgRole) ors.get(i);
          this.orgRoles[i] = new DOrgRole();
          this.orgRoles[i].setOrganizationUuid(exportString(source, "getUuid"));
          this.orgRoles[i].assignFrom(role);
        }
      } catch (Exception e) {
      }
    }
  }

  public void assignTo(IPersistent target) throws ParseException, BusinessException,
      HibernateException {
    // Do Nothing 暂时没有调用
//    if (target instanceof IOrganization)
//      super.assignTo(target);
//    if (target instanceof IOrganization) {
//      importString(target, "setCode", this.code);
//      importString(target, "setName", this.name);
//      setPropValue(target, "setOrganizationDomain", DDomain
//          .importDomain(this.organizationDomainUuid));
//      setPropValue(target, "setUpperOrganization", importOrganization(this.upperOrganizationUuid));
//      importString(target, "setRemark", this.remark);
//      importString(target, "setLevelId", this.levelId);
//
//      // orgRoles
//      IOrganizationMgr orgMgr = (IOrganizationMgr) NamingMgr.getInstance().lookupMgr(
//          IOrganizationMgr.class);
//      for (int i = 0; i < this.orgRoles.length; i++) {
//        String intfMgrName = this.orgRoles[i].get_intfMgrName();
//        IOrgRoleMgr roleMgr = orgMgr.lookupOrgRoleMgr(intfMgrName);
//        IOrgRole role = ((IOrganization) target).getRole(roleMgr);
//        if (role == null) {
//          role = orgMgr.lookupOrgRoleMgr(intfMgrName).createOrgRole((IOrganization) target);
//          orgMgr.addRoleToOrganization((IOrganization) target, role);
//        }
//        this.orgRoles[i].assignTo(role);
//      }
//    }
  }

  /**
   * 导入组织，根据organizationUuid取得组织。<br>
   * 
   * @param organizationUuid
   * @return
   * @throws BusinessException
   *           若没有找到，则报错。
   * @throws HibernateException
   */
  public static IOrganization importOrganization(String organizationUuid) throws BusinessException,
      HibernateException {
    if (organizationUuid == null || organizationUuid.equals(new String("")))
      return null;
    IOrganization org = ((IOrganizationMgr) NamingMgr.getInstance().lookupMgr(
        IOrganizationMgr.class)).getOrganization(organizationUuid);
    if (org == null)
      throw new BusinessException("系统中不存在指定的" + COrganization.CAPTION_CLASS + "(uuid="
          + organizationUuid + ")。");
    return org;
  }

  // /////////////////////////
  // 属性方法

  public String getCode() {
    return code;
  }

  public void setCode(String code) {
    this.code = code;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getOrganizationDomainUuid() {
    return organizationDomainUuid;
  }

  public void setOrganizationDomainUuid(String organizationDomainUuid) {
    this.organizationDomainUuid = organizationDomainUuid;
  }

  public String getRemark() {
    return remark;
  }

  public void setRemark(String remark) {
    this.remark = remark;
  }

  public String getUpperOrganizationUuid() {
    return upperOrganizationUuid;
  }

  public void setUpperOrganizationUuid(String upperOrganizationUuid) {
    this.upperOrganizationUuid = upperOrganizationUuid;
  }

  public String getLevelId() {
    return levelId;
  }

  public void setLevelId(String levelId) {
    this.levelId = levelId;
  }

  public DOrgRole[] getOrgRoles() {
    return orgRoles;
  }

  public void setOrgRoles(DOrgRole[] orgRoles) {
    this.orgRoles = orgRoles;
  }

}
